package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.EquipmentApply;
import com.hushao.lims.weblims.domain.EquipmentCirculate;
import com.hushao.lims.weblims.domain.EquipmentRepertory;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.EquipmentCirculateRepository;
import com.hushao.lims.weblims.service.EquipmentApplyService;
import com.hushao.lims.weblims.service.EquipmentCirculateService;
import com.hushao.lims.weblims.service.EquipmentRepertoryService;
import com.hushao.lims.weblims.util.PersonalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/22
 */
@Service
public class EquipmentCirculateServiceImpl implements EquipmentCirculateService {
    @Autowired
    private EquipmentApplyService equipmentApplyService;
    @Autowired
    private EquipmentRepertoryService equipmentRepertoryService;
    @Autowired
    private EquipmentCirculateRepository equipmentCirculateRepository;

    @Autowired
    private PersonalUtil personalUtil;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentCirculate giveBack(EquipmentCirculate equipmentCirculate,List<EquipmentApply> equipmentApplies) throws Exception {
        Integer giveBackCount=0;
        for (EquipmentApply ea:equipmentApplies){
            //修改此申请器材状态为：已借出(4)
            ea.setEquipmentApplyState("4");
            giveBackCount+=ea.getEquipmentApplyCount();
        }
        if(giveBackCount==equipmentCirculate.getEquipmentCount()){
            equipmentApplyService.update(equipmentApplies);
            return this.save(equipmentCirculate);
        }
        throw new Exception("归还失败，借出器材与归还器材不统一！");
    }

    @Transactional(rollbackFor = Exception.class)
        @Override
        public EquipmentCirculate loan(EquipmentCirculate equipmentCirculate,List<EquipmentApply> equipmentApplies) throws Exception {
            //领取人
            TeacherInfo receiver=equipmentCirculate.getEquipmentCirculateTeacher();
            for (EquipmentApply ea:equipmentApplies){
                //判断是否是申请人或者申请人给予权限
                boolean  exists=personalUtil.isJurisdiction(ea.getTeacherInfo(),receiver);
                if(exists){
                    //修改此申请器材状态为：已借出(3)
                    ea.setEquipmentApplyState("3");
                    equipmentApplyService.update(ea);
                    return this.save(equipmentCirculate);
                }

            }
            throw new Exception("借出失败，此人没有权限领取！");
        }


        @Override
        public EquipmentCirculate save(EquipmentCirculate equipmentCirculate) throws Exception {
            //查询此器材库存
            EquipmentRepertory equipmentRepertory= equipmentRepertoryService.findByEquipment(equipmentCirculate.getEquipmentInfo());
            Integer count=0;
            //修改此器材库存量
        if("0".equals(equipmentCirculate.getBorrowOrRepay())){
            //借出，库存数量去除借出数量
            count=equipmentRepertory.getEquipmentCount()-equipmentCirculate.getEquipmentCount();
        }else if ("1".equals(equipmentCirculate.getBorrowOrRepay())){
            //归还，库存数量加上归还数量
            count=equipmentRepertory.getEquipmentCount()+equipmentCirculate.getEquipmentCount();
        }

        equipmentRepertory.setEquipmentCount(count);
        //修改库存
        equipmentRepertoryService.saveOrUpdate(equipmentRepertory);
        //出库
        return  equipmentCirculateRepository.save(equipmentCirculate);
    }

}
